package com.ruoyi.sea.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.sea.domain.AlarmTrendVO;
import com.ruoyi.sea.domain.ChartVO;
import com.ruoyi.sea.service.IAlarmStatisticsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sea.domain.SeaAlarm;
import com.ruoyi.sea.service.ISeaAlarmService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 异常警告Controller
 * 
 * @author lwq
 * @date 2025-04-07
 */
@RestController
@RequestMapping("/sea/alarm")
public class SeaAlarmController extends BaseController
{
    @Autowired
    private ISeaAlarmService seaAlarmService;


    @Autowired
    private IAlarmStatisticsService iAlarmStatisticsService;

    /**
     * 查询异常警告列表
     */
    @GetMapping("/list")
    public AjaxResult list(SeaAlarm seaAlarm)
    {
        List<SeaAlarm> list = seaAlarmService.selectSeaAlarmList(seaAlarm);
        return AjaxResult.success(list);
    }

    /**
     * 导出异常警告列表
     */
    @Log(title = "异常警告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SeaAlarm seaAlarm)
    {
        List<SeaAlarm> list = seaAlarmService.selectSeaAlarmList(seaAlarm);
        ExcelUtil<SeaAlarm> util = new ExcelUtil<SeaAlarm>(SeaAlarm.class);
        util.exportExcel(response, list, "异常警告数据");
    }

    /**
     * 获取异常警告详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(seaAlarmService.selectSeaAlarmById(id));
    }

    /**
     * 新增异常警告
     */
    @Log(title = "异常警告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SeaAlarm seaAlarm)
    {
        return toAjax(seaAlarmService.insertSeaAlarm(seaAlarm));
    }

    /**
     * 修改异常警告
     */
    @Log(title = "异常警告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SeaAlarm seaAlarm)
    {
        return toAjax(seaAlarmService.updateSeaAlarm(seaAlarm));
    }

    /**
     * 删除异常警告
     */
    @Log(title = "异常警告", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(seaAlarmService.deleteSeaAlarmByIds(ids));
    }


    @GetMapping("/getAlarmStatistics")
    public AjaxResult getAlarmStatistics(String code)
    {
    	return success(iAlarmStatisticsService.selectAlarmStatistics(code));
    }

    @GetMapping("/getAlarmTrend")
    public AjaxResult getAlarmTrend(String code)
    {
        List<AlarmTrendVO> list = iAlarmStatisticsService.selectAlarmTrend(code);
        List<ChartVO> chartVOList = new ArrayList<>();
        for (AlarmTrendVO alarmTrendVO : list) {
            ChartVO chartVO = new ChartVO();
            chartVO.setDate(alarmTrendVO.getAlarmDate());
            chartVO.setValue(alarmTrendVO.getAlarmCount());
            chartVO.setType("异常数量");
            chartVOList.add(chartVO);
        }
        return success(chartVOList);
    }
}
